<template>
  <div id="app-container" style="padding:30px;">
    <el-form label-width="130px">
      <el-form-item label="版权名称">
        <el-select v-model="batch.serial" clearable placeholder="请选择">
          <el-option :value="1" label="连载中"/>
          <el-option :value="0" label="已完结"/>
        </el-select>
      </el-form-item>
      <el-form-item label="批次名称">
        <el-col :span="5">
          <el-input v-model="batch.name"/>
        </el-col>
      </el-form-item>
      <el-form-item label="分成比例">
        <el-col :span="5">
          <el-input v-model="batch.proportion"/>
        </el-col>
      </el-form-item>
      <!-- TODO 分类 -->
      <!-- TODO 版权 -->
      <!-- TODO 批次 -->
      <!-- TODO 书单 -->
      <el-form-item label="授权开始时间">
        <el-date-picker
          v-model="batch.startTime"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item label="授权结束时间">
        <el-date-picker
          v-model="batch.endTime"
          type="datetime"
          placeholder="选择结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="save()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import batch from '@/api/copyrightin/batch'
export default {
  data() {
    return {
      batch: {
        id: 0
      },
      saveBtnDisabled: false,
      firstSort: [],
      secondSort: []
    }
  },
  created() {
    this.init()
    this.queryFirstSort()
    this.viewSecondSort()
  },
  methods: {
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.view(id)
      }
    },
    view(id) {
      // 根据id进行数据查询,回显数据
      batch.view(id)
        .then(response => {
          this.batch = response.data.batch
          this.viewSecondSort(response.data.batch.secondSort)
        })
    },
    save() {
      batch.update(this.batch)
        .then(response => {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.$router.push('/bms/copyright/list')
        })
    }, queryFirstSort() {
      batch.queryFirstSort()
        .then(response => {
          this.firstSort = response.data.list
        })
    },
    querySecondSort() {
      batch.querySecondSort(this.batch.firstSort == null ? 0 : this.batch.firstSort)
        .then(response => {
          this.secondSort = response.data.list
        })
    },
    viewSecondSort(secondId) {
      batch.viewSecondSort(secondId)
    }
  }
}

</script>

